//---------------------------------------------------------------------------


#pragma hdrstop

#include "TRegistHandler.h"

//---------------------------------------------------------------------------

#pragma package(smart_init)
//---------------------------------------------------------------------------
TRegistHandler::TRegistHandler(Client *client)
{
   if(NULL==client){
        this->~TRegistHandler();
   }else{
	   _client=client;
	   Register=new Registration(client);
       Register->registerRegistrationHandler(this);
   }
}
//---------------------------------------------------------------------------
TRegistHandler::~TRegistHandler()
{
	if(NULL==_client)return;
	Register->removeRegistrationHandler();
	delete Register;
}
//---------------------------------------------------------------------------
void __fastcall TRegistHandler::PreparRegist()
{
	 if(NULL==_client)return;
	 if(_client->state()!=StateConnected)return;
	 Register->fetchRegistrationFields();
}
//---------------------------------------------------------------------------
void __fastcall TRegistHandler::ChangePassword(std::string newpassword)
{
	  if(newpassword==EmptyString)return;
	  if(NULL==_client)return;
	  if(_client->state()!=StateConnected)return;
	  Register->changePassword(_client->username(), newpassword );
}
//---------------------------------------------------------------------------
void __fastcall TRegistHandler::RemoveUser()
{
      if(_client->state()!=StateConnected)return;
	  Register->removeAccount();
}
//---------------------------------------------------------------------------
void TRegistHandler::handleRegistrationFields( const JID& from, int fields, std::string instructions )
{
	  if(FOnRegistrationFields)FOnRegistrationFields(from,fields,instructions);
}
//---------------------------------------------------------------------------
void TRegistHandler::handleRegistrationResult( const JID& from, RegistrationResult result )
{
     if(FOnRegistrationResult)FOnRegistrationResult(from,result);
}
//---------------------------------------------------------------------------
void TRegistHandler::handleAlreadyRegistered( const JID& from )
{
     if(FOnAlreadyRegistered)FOnAlreadyRegistered(from);
}
//---------------------------------------------------------------------------
void TRegistHandler::handleDataForm( const JID& from, const DataForm& form )
{
	 if(FOnDataForm)FOnDataForm(from,form);
}
//---------------------------------------------------------------------------
void TRegistHandler::handleOOB( const JID& from, const OOB& oob )
{
	 if(FOnOOB)FOnOOB(from,oob);
}
//---------------------------------------------------------------------------
void __fastcall TRegistHandler::CreateUser(int fields,std::string username,std::string password)
{
	if(username==EmptyString||password==EmptyString)return;
	if(NULL==_client)return;
	if(_client->state()!=StateConnected)return;
	RegistrationFields vals;
	vals.username = username;
	vals.password = password;
	Register->createAccount( fields, vals );
}
